package com.longcai.service.impl.clyjheadlines;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.homepage.ClyjHeadlines;
import com.longcai.mapper.clyjheadlines.ClyjHeadlinesMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjheadlines.ClyjHeadlinesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 平台头条 服务实现类1
 * </p>
 *
 * @author M
 * @since 2019-03-27
 */
@Service
public class ClyjHeadlinesServiceImpl extends ServiceImpl<ClyjHeadlinesMapper, ClyjHeadlines> implements ClyjHeadlinesService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjHeadlinesMapper clyjHeadlinesMapper;

    /**
     * 获取头条列表
     *
     * @param clyjHeadlines
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse headList(ClyjHeadlines clyjHeadlines, PageHelper pageHelper) {
        //分页
        Page<ClyjHeadlines> page = new Page<ClyjHeadlines>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjHeadlines> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clyjHeadlines.getTitle())) {
            queryWrapper.like("title", clyjHeadlines.getTitle());
        }
        //查询状态为0的头条
        queryWrapper.eq("status", 0);
        IPage<ClyjHeadlines> sysRoleIPage = this.clyjHeadlinesMapper.selectPage(page, queryWrapper);
        PageTableResponse pageTableResponse = new PageTableResponse(sysRoleIPage.getTotal(),
                sysRoleIPage.getTotal(), sysRoleIPage.getRecords());
        return pageTableResponse;
    }

    /**
     * 根据名称查询头条信息
     *
     * @param title
     * @return
     */
    @Override
    public ClyjHeadlines getTitle(String title) {
        try {
            QueryWrapper<ClyjHeadlines> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("title", title);
            ClyjHeadlines clyjHeadlines = this.clyjHeadlinesMapper.selectOne(queryWrapper);
            logger.debug("查询头条{}", clyjHeadlines);
            return clyjHeadlines;
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        }
        return null;
    }

    /**
     * 新增
     *
     * @param clyjHeadlines
     * @return
     */
    @Override
    public ClyjHeadlines saveHead(ClyjHeadlines clyjHeadlines) {
        try {
            ClyjHeadlines clyjHeadlines1 = clyjHeadlines;
            clyjHeadlines1.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjHeadlines1.setStatus(0);
            clyjHeadlines1.setCreateTime(new Date());
            clyjHeadlines1.setLineSort("00002");
            Integer integer = this.clyjHeadlinesMapper.insert(clyjHeadlines1);
            logger.debug("新增成功++++++++++++", integer);
            return clyjHeadlines1;
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
            e.getMessage();
        }

        return null;
    }

    /**
     * 根据id获取头条信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getById(String id) {

        try {
            ClyjHeadlines clyjHeadlines = this.clyjHeadlinesMapper.selectById(id);
            logger.debug("查询成功++++++++++++++", clyjHeadlines);
            return ApiResult.success(clyjHeadlines);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改头条信息
     *
     * @param clyjHeadlines
     * @return
     */
    @Override
    public Integer updateHead(ClyjHeadlines clyjHeadlines) {

        try {
            return this.clyjHeadlinesMapper.updateById(clyjHeadlines);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除头条
     *
     * @param id
     */
    @Override
    public void deleteById(String id) {
        try {
            QueryWrapper<ClyjHeadlines> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            ClyjHeadlines clyjHeadlines = new ClyjHeadlines();
            //修改状态为1
            clyjHeadlines.setStatus(1);
            Integer integer = this.clyjHeadlinesMapper.update(clyjHeadlines, queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
